//
//  TabbarViewController.swift
//  TimMusic
//
//  Created by nkl on 2024/10/8.
//

import Foundation
import SnapKit
import UIKit

class TMTabbarViewController: UITabBarController, TMTabBarDelegate {
    static var rootTabbar: TMTabbarViewController? {
        if let rootVc = UIApplication.shared.delegate?.window??.rootViewController,
           let tabVc = rootVc as? TMTabbarViewController {
            return tabVc
        }
        return nil
    }

    lazy var customBar: TMTabBar = {
        let bar = TMTabBar()
        bar.delegate = self
        bar.playBtn.iconButton.addTarget(self, action: #selector(showPlayDetailViewController), for: .touchUpInside)
        bar.playBtn.pauseButton.addTarget(self, action: #selector(pauseButtonAction), for: .touchUpInside)
        return bar
    }()

    lazy var playDetailVc: PlayDetailViewController = .init()

    override func viewDidLoad() {
        super.viewDidLoad()
        tabBar.removeFromSuperview()
        addCustomBar()
        addChildrens()
        makeConstraints()
    }

    func addCustomBar() {
        view.addSubview(customBar)
        view.addSubview(playDetailVc.view)
        customBar.changeStatus(show: true)
    }

    func makeConstraints() {
        customBar.snp.makeConstraints { make in
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
            make.leading.trailing.equalToSuperview()
            make.height.equalTo(tabbarHeight)
        }

        playDetailVc.view.snp.makeConstraints { make in
            make.top.equalTo(view.snp.bottom)
            make.leading.trailing.equalToSuperview()
            make.height.equalTo(UIScreen.kScreenHeight)
        }
    }

    func addChildrens() {
        let homeVc = HomeViewController()
        let homeNav = BasicNavigationController(rootViewController: homeVc)
        let widgetsVc = WidgetsViewController()
        let widgetsNav = BasicNavigationController(rootViewController: widgetsVc)
        let settingVc = SettingViewController()
        let settingNav = BasicNavigationController(rootViewController: settingVc)
        addChild(homeNav)
        addChild(widgetsNav)
        addChild(settingNav)
    }

    func didSelectedTabbarItem(idx: Int) {
        selectedIndex = idx
    }

    @objc func showPlayDetailViewController() {
        showPlayDetail()
    }

    @objc func pauseButtonAction() {
        PlayDetailManager.shared.pauseOrPlay()
    }

    func hideSelectedBar() {
        customBar.hStack.isHidden = true
        customBar.changeStatus(show: false)
    }

    func showSelectedBar() {
        customBar.hStack.isHidden = false
        customBar.changeStatus(show: true)
    }

    func showPlayDetail() {
        UIView.animate(withDuration: 0.2) {
            self.playDetailVc.view.snp.updateConstraints { make in
                make.top.equalTo(self.view.snp.bottom).offset(-UIScreen.kScreenHeight)
            }
            self.view.setNeedsLayout()
            self.view.layoutIfNeeded()
        }
    }

    func hidePlayDetail() {
        UIView.animate(withDuration: 0.2) {
            self.playDetailVc.view.snp.updateConstraints { make in
                make.top.equalTo(self.view.snp.bottom)
            }
            self.view.setNeedsLayout()
            self.view.layoutIfNeeded()
        }
    }
}
